package com.luhuiguo.cms.server.converter;

import org.apache.commons.beanutils.Converter;

import com.google.appengine.api.datastore.Blob;

public class BlobConverter implements Converter {

    @SuppressWarnings("unchecked")
    @Override
    public Object convert(Class type, Object value) {
        if (null == value) {
            return null;
        }
        if (type.equals(Blob.class)) {
            if (value instanceof byte[]) {
                byte[] data = (byte[]) value;
                return new Blob(data);
            } else {
                return null;
            }
        } else {
            if (value instanceof Blob) {
                Blob blob = (Blob) value;
                return blob.getBytes();
            } else {
                return null;
            }
        }

    }

}
